package rest.jbpm.services;


import javax.ws.rs.*;
import javax.ws.rs.core.*;
import rest.jbpm.JbpmService;
import rest.jbpm.ProcessEngineException;
import rest.jbpm.wrappers.lists.JobList;

@Path("mgmt")
public class ManagementService extends JbpmService {
	
	@Context UriInfo uriInfo;
	
	@GET
	@Path("job/{jobId}/execute")
	public String execute(@PathParam("jobId") String jobId) {
		processEngine.getManagementService().executeJob(jobId);
		return null;
	}
	
	@GET
	@Path("jobs")
	public JobList getJobs() {
		
		if(processEngine == null)
			throw new ProcessEngineException("Fire! fire! mayday! mayday! we're going down!.. JBPM process engine is down!");
		
		return new JobList(processEngine.getManagementService().createJobQuery().list(),
				uriInfo.getBaseUri().toString());
		
	}
	
	@GET
	@Path("job/forProcessInstance/{processInstanceId}")
	public JobList getJobByProcessId(@PathParam("processInstanceId") String processInstanceId) {
		
		if(processEngine == null)
			throw new ProcessEngineException("Fire! fire! mayday! mayday! we're going down!.. JBPM process engine is down!");
		
		return new JobList(processEngine.getManagementService().createJobQuery().processInstanceId(processInstanceId).list(),
				uriInfo.getBaseUri().toString());
	}
	
}
